using M13.DataStructure;
using M13.Game.Item;
using System;
using System.Collections.Generic;
using System.Reflection;
using M13.Game.Item.Weapon;
using UnityEngine;

namespace M13.Utility.Registrar
{
    /*
     * 这里重新写一下
     */
    public class ItemRegistrar<t> where t : IItem, new()
    {
        public static t Register(int id, string namespaceId, ItemType itemType, int maxStackCount, Sprite itemicon, GameObject itemModal, List<Item> itemFields, UniqueIdDictionary<int, string> idKeyValues)
        {
            ItemProperties itemProperties = new ItemProperties(id, namespaceId, itemType, maxStackCount, itemicon, itemModal);
            t t = Registered(itemProperties);
            itemFields.Add(t as Item);
            idKeyValues.Add(id, namespaceId);
            return t;
        }
        private static t Registered(ItemProperties info)
        {
            Type myType = typeof(t);
            t myInstance = (t)Activator.CreateInstance(myType);
            MethodInfo methodInfo = myType.GetMethod("Init", BindingFlags.NonPublic | BindingFlags.Instance);
            object[] myParaeters = new object[] { info };
            if (myInstance != null)
                methodInfo.Invoke(myInstance, myParaeters);
            return myInstance;
        }
    }

    public class WeaponRegistrar<t> where t : IWeapon, new()
    {
        public static t Registered(int id, string namespaceId, ItemType itemType, int maxStackCount, Sprite itemicon, GameObject itemModal, int weaponDamage, int weaponStunPower, int maxDurabilityValue, WeaponResourceConfig weaponConfig, List<Item> itemDatas, UniqueIdDictionary<int, string> uniqueIdDictionary)
        {
            ItemProperties itemProperties = new ItemProperties(id, namespaceId, itemType, maxStackCount, itemicon, itemModal);
            WeaponProperties weaponProperties = new WeaponProperties(weaponDamage, weaponStunPower, maxDurabilityValue);
            t t = Registered(itemProperties, weaponProperties, weaponConfig);
            itemDatas.Add(t as Weapon);
            uniqueIdDictionary.Add(id, namespaceId);
            return t;
        }
        private static t Registered(ItemProperties itemProperties, WeaponProperties weaponProperties, WeaponResourceConfig weaponConfig)
        {
            Type myType = typeof(t);
            t myInstance = (t)Activator.CreateInstance(myType);
            MethodInfo methodInfo = myType.GetMethod("Init", BindingFlags.Public | BindingFlags.Instance);
            object[] myParaeters = new object[] { itemProperties, weaponProperties, weaponConfig };
            if (myInstance != null)
                methodInfo.Invoke(myInstance, myParaeters);
            return myInstance;
        }
    }
}